#ifndef XgAutoGetAttrIncluded
#define XgAutoGetAttrIncluded
library XgAutoAttrSystem
globals
hashtable XgAutoAttrSystem_htb = InitHashtable()
endglobals
<? local slk = require "slk"
bak_tostring = tostring
function tostring(s)
 if type(s) == "nil" then
   s = ""
 end
 return bak_tostring(s)
end
bak_tonumber = tonumber
tonumber=function (s)
 if type(s) == "nil" then
   s = 0
 end
 return bak_tonumber(s)
end
xg_item_attr = {
["num"]=0,
["class"]="",
['Item']={},
['AttrVal']={},
['Attr']={}
}
function string.change(str,start,ed,new)
    return str:sub(1,start-1)..new..str:sub(ed)
end
function string.split(str,f)
    local p,arr,s,q,ls,le = 1,{},""
    f = tostring(f)
    if f == '' then return {} end
    str = tostring(str) .. f
    str = str:gsub(
            "|[cC]([a-z0-9A-Z]+)",
            function(w)
                return w:sub(9) --返回多余的非颜色数值
            end)
    str = str:gsub('|[rR]','')
    str = str:gsub('\t','')
    while true do
        ls,le = str:find(f,p,true)
        if ls == nil then break end
        s = str:sub(p, ls-1) --从起始位置到分隔符
        table.insert(arr,s)
        p=le+1
    end
    return arr
end
function XG_IsSit(id,str) --存在属性
    str = str:gsub("\n","|n")
    str = str:gsub('%s','') --空格
    
    local s = string.split(str,'|n')
    local retn,r,j,i,f = false,0
    local Ls,Le
   	local result = ""
    i=1
--xg_item_attr[1]["name"] = "物理吸血%+([0-9]+)(%%)"
    while i <= #s do
        j=1
        
        while j <= xg_item_attr["num"] do
        
            local ret = { s[ i ]:match( xg_item_attr[j]["name"] ) }
            local paramCount = #ret

            if paramCount > 0 then
                
                if type(xg_item_attr[id])~="table" then
                    xg_item_attr[id]={}
                end

                local rsz= ret[1]

                --附加规则代码
                if xg_item_attr[j]['code'] ~= '' then
                    Gn = xg_item_attr[j]['code']
                    local Gn = Gn

                    for pi = 1, paramCount do
                        if ret[pi]:sub(1,1) == '+' then
                            ret[pi] = ret[pi]:sub(2)
                        end
                        Gn = Gn:gsub( "#" .. pi, ret[pi] )
                    end
                    
                    rsz = load("return ( " .. Gn .. " )" ) ()
                    
                    rsz = load("return ( " .. rsz .. " )" ) ()

                end

                local key = xg_item_attr[j] ["int"]

                rsz = tonumber(rsz) + tonumber( xg_item_attr[id][ key ] )
                xg_item_attr[id][ key ] = rsz

                for n=1,#xg_item_attr['Attr'] + 1 do
                    if n > #xg_item_attr['Attr'] then --不存在时插入项目
                        table.insert(xg_item_attr['Attr'], key )
                        table.insert(xg_item_attr['AttrVal'], rsz)
                        break
                    elseif xg_item_attr['Attr'][n] == key then
                        xg_item_attr['AttrVal'][n] = rsz
                        break
                    end
                end--for n
                
              break
              
            end --if r>0
            j=j+1
        end
        i=i+1
    end
    
        if #xg_item_attr['Attr'] >0 then
             result = result.."call SaveInteger(XgAutoAttrSystem_htb,'".. id .."',0,".. #xg_item_attr['Attr'] ..")\r\n"
		end
		for i=1,#xg_item_attr['Attr'] do
				result = result.."call SaveInteger(XgAutoAttrSystem_htb,'".. id .."',".. i ..","..tostring(xg_item_attr['Attr'][i])..")\r\n"..
				"call SaveReal(XgAutoAttrSystem_htb,'".. id .."',"..tostring(xg_item_attr['Attr'][i])..","..tostring(xg_item_attr['AttrVal'][i])..")\r\n"
		end
    xg_item_attr['Attr']={}
    xg_item_attr['AttrVal']={}
    return result
end
    function XG_AutoAttr_Start_Lua()
		local result = ""
        for id, obj in pairs(slk.item) do
                if  obj.class == xg_item_attr["class"] or xg_item_attr["class"]==''  then
					            result = result..XG_IsSit(id,obj.Ubertip)
                end
            end

        return 'DoNothing()\r\n'..result
    end

    function XG_AutoAttr_SetClass_Lua(str)
        xg_item_attr["class"] = str
        return 'DoNothing()'
    end

    function XG_AutoAttr_AddAttr_Lua(s,a,v,c)
        local n = xg_item_attr["num"]+1
        xg_item_attr["num"] = n
        xg_item_attr[n]=
            {
                ["name"] =  s,
                ["int"] = a,
                ['code'] = c
            }
            return 'XG_AutoAttr_AddAttr('..a..',"'..v..'")'
    end
 ?>

 //添加属性 key value
function XG_AutoAttr_AddAttr takes integer a, string v returns nothing
    local integer i = LoadInteger(XgAutoAttrSystem_htb, 0, 0) + 1
    call SaveInteger(XgAutoAttrSystem_htb, 0, i, a)
    //call SaveStr(XgAutoAttrSystem_htb, 0, i, v)
    //key绑定value
    call SaveStr(XgAutoAttrSystem_htb, 0, a, v)
    call SaveInteger(XgAutoAttrSystem_htb, 0, 0, i)
endfunction

//获取物品i属性数量
function XG_AutoAttr_GetAttrNum takes integer i returns integer
    return LoadInteger(XgAutoAttrSystem_htb, i, 0)
endfunction

//获取 物品i 的 第a条 属性的Key
function XG_AutoAttr_GetAttrKey takes integer i, integer a returns integer
    return LoadInteger(XgAutoAttrSystem_htb, i, a)
endfunction

//获取物品i 的 第a条 属性值
function XG_AutoAttr_GetAttrVal takes integer i,integer a returns real
    return LoadReal(XgAutoAttrSystem_htb, i, a)
endfunction

//获取key绑定的属性名
function XG_AutoAttr_GetAttrName takes integer i returns string
    return LoadStr(XgAutoAttrSystem_htb, 0, i)
endfunction

endlibrary
#endif
